﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace Editor
{
    public partial class TileListWindow : Form
    {
        private Image _Tiles;
        private EditorCursor _cursor;
        private Graphics _mainGraphicsControl;
        private Graphics _cursorGraphicsControl;
        private Bitmap _background;
        private Size _boxSize = new Size(672, 384);
        private Point _tileScroll = new Point(0, 0);
        private BufferedGraphicsContext _graphicsBuffer;

        public delegate void PassBitmap(Bitmap tileImage, int X, int Y);
        public event PassBitmap TileSelected;

        private int _tileXLocation
        {
            get { return (_tileScroll.X / -32) + _cursor.OldX; }
        }

        private int _tileYLocation
        {
            get { return (_tileScroll.Y / -32) + _cursor.OldY; }
        }

        public TileListWindow(ref Image Tiles)
        {
            _graphicsBuffer = BufferedGraphicsManager.Current;
            _Tiles = Tiles;
            InitializeComponent();
            _background = new Bitmap(_Tiles);
            _mainGraphicsControl = Graphics.FromImage(_background);
            _cursorGraphicsControl = tileSheetPanel.CreateGraphics();
            _graphicsBuffer.Allocate(_cursorGraphicsControl, new Rectangle(tileSheetPanel.Location, tileSheetPanel.Size));
            _cursor = new EditorCursor(ref _cursorGraphicsControl, ref _background);
            SetupCursor();
            DoubleBuffered = true;
        }
        private void SetupCursor()
        {
            _cursor.UpdatePosition(0, 0);
        }
        private void DrawScreen()
        {
            _mainGraphicsControl.Clear(Color.Black);
            _mainGraphicsControl.DrawImageUnscaledAndClipped(_Tiles, new Rectangle(_tileScroll, _boxSize));
            _mainGraphicsControl.Flush();
            SetupCursor();
        }
        private void TileListWindow_Activated(object sender, EventArgs e)
        {
            DrawScreen();
        }

        private void tileSheetPanel_mouseMove(object sender, MouseEventArgs e)
        {
            _cursor.UpdatePosition((int)(e.Location.X / 32), (int)(e.Location.Y / 32));
        }

        private void TileListWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
                return;
            }
            else
            {
                _graphicsBuffer.Invalidate();
                _graphicsBuffer.Dispose();
            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            _tileScroll.X = e.NewValue * -32;
            _mainGraphicsControl.Clear(Color.Black);
            _mainGraphicsControl.DrawImage(_Tiles, new Rectangle(_tileScroll, _Tiles.Size));
            _mainGraphicsControl.Flush();
            _cursor.Draw();
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            _tileScroll.Y = e.NewValue * -32;
            _mainGraphicsControl.Clear(Color.Black);
            _mainGraphicsControl.DrawImage(_Tiles, new Rectangle(_tileScroll, _Tiles.Size));
            _mainGraphicsControl.Flush();
            _cursor.Draw();
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            try
            {
                verticalScrollBar.Value += e.Delta / -120;
            }
            catch { }
            base.OnMouseWheel(e);
        }

        private void vScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            _tileScroll.Y = verticalScrollBar.Value * -32;
            _mainGraphicsControl.Clear(Color.Black);
            _mainGraphicsControl.DrawImage(_Tiles, new Rectangle(_tileScroll, _Tiles.Size));
            _mainGraphicsControl.Flush();
            _cursor.Draw();
        }

        private void tileSheetPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Point drawPoint = new Point(-(_tileScroll.X - _cursor.OldX * 32), -(_tileScroll.Y - _cursor.OldY * 32));
            System.Drawing.Size size = new Size(32, 32);
            Bitmap image = new Bitmap(32, 32);
            Graphics tempGraphicsControl = Graphics.FromImage(image);
            tempGraphicsControl.DrawImage(_Tiles, new Rectangle(new Point(0, 0), size), new Rectangle(drawPoint, size), GraphicsUnit.Pixel);
            tempGraphicsControl.Dispose();
            this.TileSelected(image, -(_tileScroll.X/32) + _cursor.OldX, -(_tileScroll.Y/32) + _cursor.OldY);
            this.Hide();
        }


    }
}
